# Create your views here.

from django.http import HttpResponse
from django.template import Context, loader
from django.shortcuts import render_to_response

def patient_list(request):
    return HttpResponse("Hello World - SEMR - patient_list")

def patient_details(request, patient_id):
    #return HttpResponse("Hello World - SEMR - patient_details for MRN - %s" % patient_id)
    from SPARQLWrapper import SPARQLWrapper, JSON

    sparql = SPARQLWrapper("http://127.0.0.1:8890/sparql")
    query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX docs: <http://www.docsnotes.com/Ontology/>
SELECT ?l ?o
FROM <http://localhost:8890/DAV>
WHERE{
?p rdfs:label "Medical Records Number"@en.
?s ?px ?o.
?px rdfs:label ?l.
?s ?p %s.
}
""" % patient_id
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()

    return render_to_response('patient_details.html', {"patient_id":patient_id, "results":results})

def new_patient(request):
    from SPARQLWrapper import SPARQLWrapper, JSON

    sparql = SPARQLWrapper("http://127.0.0.1:8890/sparql") #thsi is the ontology that we need
    query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX docs: <http://www.docsnotes.com/Ontology/>
SELECT *
FROM <http://localhost:8890/DAV>
WHERE{
_:s rdfs:label "Person"@en.
_:p rdfs:domain _:s.
_:p rdfs:label ?propertylabel.
_:p rdfs:range ?range.
}
"""
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()

    for result in results["results"]["bindings"]:
        print result["propertylabel"]["value"]
        print result["range"]["value"]
    
    return render_to_response('new_patient.html', {"results":results})

    #set ontology
    #create new person
    #get properties that have a domain of person (or superclasses)
    #get the ranges for that property

    #make helpful data entry widgets
    #add javascript validation


